package com.aiti.lulian.service.impl;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.aiti.base.core.base.service.iml.BaseSerivceImpl;
import com.aiti.lulian.entity.SysArea;
import com.aiti.lulian.mapper.SysAreaMapper;
import com.aiti.lulian.service.ISysAreaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author chl
 * @since 2022-05-12
 */
@Service
public class SysAreaServiceImpl extends BaseSerivceImpl<SysAreaMapper, SysArea> implements ISysAreaService {

    @Autowired
    private SysAreaMapper sysAreaMapper;

    @Override
    public List<SysArea> findListPage(Page page, SysArea sysArea) {
        return sysAreaMapper.findListPage(page, sysArea);
    }

    @Override
    public List<Tree<String>> getAreaTree() {
        List<SysArea> sysAreas = sysAreaMapper.selectList(new QueryWrapper<SysArea>().like("area_code", "37").eq("is_delete", "0"));

        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        // 自定义属性名 都有默认值的
        treeNodeConfig.setWeightKey("areaCode");
        treeNodeConfig.setIdKey("baseId");
        treeNodeConfig.setParentIdKey("baseParentId");
        // 最大递归深度
        treeNodeConfig.setDeep(3);

        //转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        return TreeUtil.<SysArea, String>build(sysAreas, null, treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getBaseId());
                    tree.setParentId(treeNode.getBaseParentId());
                    tree.setWeight(treeNode.getAreaCode());
                    tree.setName(treeNode.getBaseName());
                });
    }

}
